package com.wxb.plugin.core.parser;

import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMethod;
import com.wxb.plugin.core.gen.StringUtil;

import static com.wxb.plugin.core.gen.QualifyClassName.*;

/**
 * @author wxb
 * @createTime 2025/2/27 16:07
 */
public class TextParser extends Parser {

    /**
     * unicode 专用字符区域的码点
     */
    static final  String CUSTOM_CHAR1 = "\\ue000\\ue001\\ue002";
    static final  String CUSTOM_CHAR2 = "\\uE000\\uE001\\uE002";

    public static String getClassName(PsiClass aClass) {

        PsiAnnotation annotation = aClass.getAnnotation(Tag);
        if (annotation != null) {
            return getText(annotation,  "name");
        }
        return "";
    }

    public static String getClassNote(PsiClass aClass) {
        String note = "";
        PsiAnnotation annotation = aClass.getAnnotation(Api);
        if (annotation != null) {
            note = getText(annotation, "value");
            if (note != null && !note.isEmpty()) {
                return note;
            }
        }
        annotation = aClass.getAnnotation(Tag);
        if (annotation != null) {
            note = getText(annotation, "description");
        }
        return note;
    }

    public static String getMethodNote(PsiMethod method) {
        PsiAnnotation annotation;
        if (method.getAnnotation(ApiOperation) != null) {
            annotation = method.getAnnotation(ApiOperation);
        } else {
            annotation = method.getAnnotation(Operation);
        }
        String text = getText(annotation, "summary","value", "name", "description");
        return StringUtil.isBlank(text) ? method.getName() : text;
    }



    public static String getEntityNote(PsiClass psiClass) {
        PsiAnnotation annotation;
        if (psiClass.getAnnotation(ApiModel) != null) {
            annotation = psiClass.getAnnotation(ApiModel);
        } else {
            annotation = psiClass.getAnnotation(Schema);
        }
        return getText(annotation, "value", "name", "description");
    }
    public static boolean hideField(PsiField field) {
        PsiAnnotation annotation;
        if (field.getAnnotation(ApiModelProperty) != null) {
            annotation = field.getAnnotation(ApiModelProperty);
        } else {
            annotation = field.getAnnotation(Schema);
        }
        if (annotation != null && annotation.findAttributeValue("hidden") != null) {
            return "true".equals(annotation.findAttributeValue("hidden").getText());
        }
        return false;
    }


    public static String getValue(PsiAnnotation annotation, String... values) {
        String text = getText(annotation, values);
        if(text != null && !text.contains(CUSTOM_CHAR1) && !text.contains(CUSTOM_CHAR2)) {
            return text;
        }
        return "";
    }
}
